package com.wingsboat.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wingsboat.common.core.web.page.PageDomain;
import com.wingsboat.common.core.web.page.TableSupport;
import com.wingsboat.common.security.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wingsboat.common.log.annotation.Log;
import com.wingsboat.common.log.enums.BusinessType;
import com.wingsboat.common.security.annotation.RequiresPermissions;
import com.wingsboat.system.domain.SysSleeper;
import com.wingsboat.system.service.ISysSleeperService;
import com.wingsboat.common.core.web.controller.BaseController;
import com.wingsboat.common.core.web.domain.AjaxResult;
import com.wingsboat.common.core.web.page.TableDataInfo;

/**
 * 轨枕信息Controller
 * 
 * @author wangzhong
 * @date 2024-06-09
 */
@RestController
@RequestMapping("/detectionData")
public class SysSleeperController extends BaseController
{
    @Autowired
    private ISysSleeperService sysSleeperService;

    /**
     * 查询轨枕信息列表
     */
    @RequiresPermissions("system:sleeper:list")
    @GetMapping("/queryPagingSleeperlist")
    public JSONObject list(SysSleeper sysSleeper)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String url = "http://59.110.8.161:17022/detectionData/queryPagingSleeperlist";
        Map<String,Object> queryParams = new HashMap<>();
        queryParams.put("pageNum",pageNum);
        queryParams.put("pageSize",pageSize);
        queryParams.put("lineNo",sysSleeper.getLineNo());
        queryParams.put("startMileage",0.00);
        queryParams.put("endMileage",0.00);
        String s = HttpUtil.get(url, queryParams);
        JSONObject jsonObject =  JSON.parseObject(s);
//        TableDataInfo tableDataInfo = new TableDataInfo();
//        tableDataInfo.setTotal(jsonObject.get("total"));
//        tableDataInfo.setRows((List<?>) jsonObject.get("rows"));
        return jsonObject;
    }

    /**
     * 导出轨枕信息列表
     */
    @RequiresPermissions("system:sleeper:export")
    @Log(title = "轨枕信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSleeper sysSleeper)
    {
        List<SysSleeper> list = sysSleeperService.selectSysSleeperList(sysSleeper);
        ExcelUtil<SysSleeper> util = new ExcelUtil<SysSleeper>(SysSleeper.class);
        util.exportExcel(response, list, "轨枕信息数据");
    }

    /**
     * 获取轨枕信息详细信息
     */
    @RequiresPermissions("system:sleeper:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysSleeperService.selectSysSleeperById(id));
    }

    /**
     * 新增轨枕信息
     */
    @RequiresPermissions("system:sleeper:add")
    @Log(title = "轨枕信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSleeper sysSleeper)
    {
        return toAjax(sysSleeperService.insertSysSleeper(sysSleeper));
    }

    /**
     * 修改轨枕信息
     */
    @RequiresPermissions("system:sleeper:edit")
    @Log(title = "轨枕信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSleeper sysSleeper)
    {
        return toAjax(sysSleeperService.updateSysSleeper(sysSleeper));
    }

    /**
     * 删除轨枕信息
     */
    @RequiresPermissions("system:sleeper:remove")
    @Log(title = "轨枕信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysSleeperService.deleteSysSleeperByIds(ids));
    }


    /**
     * 获取轨枕列表
     * @param lineNo
     * @param pixelNo
     * @param moveSign
     * @param moveNum
     * @return
     */
    @GetMapping("querySleeperlist")
    public AjaxResult querySleeperList(String lineNo,Integer pixelNo, String moveSign,Integer moveNum){
        String url = "http://59.110.8.161:17022/detectionData/querySleeperlist";
        Map<String,Object> queryParams = new HashMap<>();
        queryParams.put("lineNo",lineNo);
        queryParams.put("pixelNo",pixelNo);
        queryParams.put("moveSign",moveSign);
        queryParams.put("moveNum",moveNum);
        String s = HttpUtil.get(url, queryParams);
        JSONObject jsonObject =  JSON.parseObject(s);
        return success(jsonObject.get("rows"));
    }

    /**
     * 获取最大最小轨枕
     * @param lineNo
     * @return
     */
    @GetMapping("querySleeperMiniMaxiPixelNoData")
    public AjaxResult querySleeperMiniMaxiPixelNoData(String lineNo){
        String url = "http://59.110.8.161:17022/detectionData/querySleeperMiniMaxiPixelNoData";
        Map<String,Object> queryParams = new HashMap<>();
        queryParams.put("lineNo",lineNo);
        queryParams.put("mark","MinMax");
        String s = HttpUtil.get(url, queryParams);
        JSONObject jsonObject =  JSON.parseObject(s);
        return success(jsonObject.get("rows"));
    }

    /**
     * 根据轨枕号查询轨枕信息
     * @param lineNo
     * @param sleeperNo
     * @return
     */
    @GetMapping("querySleeperDataBySleeperNo")
    public AjaxResult querySleeperDataBySleeperNo(String lineNo,String sleeperNo){
        String url = "http://59.110.8.161:17022/detectionData/querySleeperDataBySleeperNo";
        Map<String,Object> queryParams = new HashMap<>();
        queryParams.put("lineNo",lineNo);
        queryParams.put("sleeperNo",sleeperNo);
        String s = HttpUtil.get(url, queryParams);
        JSONObject jsonObject =  JSON.parseObject(s);
        return success(jsonObject.get("data"));
    }



    @GetMapping("queryWaveFormList")
    public JSONObject queryWaveFromList(String lineNo,String pageNum,String pageSize,String startKm,String endKm){
        String url = "http://59.110.8.161:17022/detectionData/queryWaveFormList";
        Map<String,Object> queryParams = new HashMap<>();
        queryParams.put("lineNo",lineNo);
        queryParams.put("pageNum",pageNum);
        queryParams.put("startKm",startKm!=null?startKm:"0.0");
        queryParams.put("endKm",endKm!=null?endKm:"0.0");
        queryParams.put("pageSize",pageSize);
        String s = HttpUtil.get(url, queryParams);
        JSONObject jsonObject =  JSON.parseObject(s);
        return jsonObject;
    }


}
